<template>
	<view class="box">
		<view class="row">
			<view class="row-left">
				任务名称：
			</view>
			<view class="row-right">
				<view class="value">
					{{name[0]}}
				</view>
				<view class="tss" @click="toListData('name')">请选择 > </view>
			</view>
		</view>
		<view class="row">

			<uni-datetime-picker v-model="taskStartTime" type="datetime" rangeSeparator="至">
				<view class="row">
					<view class="row-left">
						开始日期：
					</view>
					<view class="row-right">
						<view class="value">
							{{taskStartTime}}
						</view>
						<view class="tss">请选择 > </view>
					</view>
				</view>

			</uni-datetime-picker>

		</view>
		<view class="row"><uni-datetime-picker v-model="taskEndTime" type="datetime" rangeSeparator="至">
				<view class="row">
					<view class="row-left">
						截至日期：
					</view>
					<view class="row-right">
						<view class="value">
							{{taskEndTime}}
						</view>
						<view class="tss">请选择 > </view>
					</view>
				</view>

			</uni-datetime-picker>
		</view>
		<!-- <view class="row">
			<uni-datetime-picker v-model="taskDate" type="datetimerange" rangeSeparator="至">
				<view class="row">
					<view class="row-left">
						开始日期：
					</view>
					<view class="row-right">
						<view class="value">{{taskDate[0]}}</view>
						<view class="tss">请选择 > </view>
					</view>
				</view>
				<view class="row">
					<view class="row-left">
						截至日期：
					</view>
					<view class="row-right">
						<view class="value">{{taskDate[1]}}</view>
						<view class="tss">请选择 > </view>
					</view>
				</view>
			</uni-datetime-picker>
		</view> -->
		<view class="row">
			<view class="row-left">
				负责人：
			</view>
			<view class="row-right">
				<view class="value">
					{{responsibleUser[0]}}
				</view>
				<view class="tss" @click="toListData('leader')">请选择 > </view>
			</view>
		</view>
		<view class="row">
			<view class="row-left">
				抄送人：
			</view>
			<view class="row-right">
				<view class="value">
					<span v-for="(item,index) in carbonCopyUsers"
						:key="index">{{item}}<span>{{ index+1==carbonCopyUsers.length?'':'、'}}</span></span>
				</view>
				<view class="tss" @click="toListData('csMan',carbonCopyUsers)">请选择 > </view>
			</view>
		</view>
		<!-- 选择日期 -->
		<view>
			<uv-calendars ref="calendars" mode="multiple" @confirm="confirm" />
		</view>

		<view class="btnBox">
			<button @click="saveHandler">保存</button>
		</view>
	</view>
</template>

<script>
	import {
		baseURL
	} from '@/api/env.js'
	export default {
		data() {
			return {
				name: uni.getStorageSync('taskName'),
				taskDate: [],
				taskStartTime: '',
				taskEndTime: '',
				id: null,
				taskConfigId: null,
				responsibleUser: uni.getStorageSync('leader'),
				carbonCopyUsers: uni.getStorageSync('csMan'),
				manOptions: []
			};
		},
		onShow() {

			this.name = uni.getStorageSync('taskName')
			this.responsibleUser = uni.getStorageSync('leader')
			this.carbonCopyUsers = uni.getStorageSync('csMan')
		},
		onLoad(option) {
			let params = JSON.parse(option.item)
			this.id = params.id
			if (this.id == null) {
				uni.setNavigationBarTitle({
					title: '新增安排'
				})
			} else {
				uni.setNavigationBarTitle({
					title: '修改安排'
				})
			}
			this.taskConfigId = params.taskConfigId
			this.name = params.name
			this.taskDate = params.taskDate
			this.taskStartTime = params.taskStartTime
			this.taskEndTime = params.taskEndTime
			this.responsibleUser = params.responsibleUser.name
			this.createUserName = params.createUserName
		},
		methods: {
			bindTimeChange(e) {
				this.taskStartTime = e.detail.value
			},
			bindTimeChange2(e) {
				this.taskEndTime = e.detail.value
			},
			open() {
				this.$refs.calendars.open()
			},
			confirm(e) {
				this.taskDate = e.multiple.data
			},
			// 去选择
			toListData(v) {
				console.log('v', v);
				uni.navigateTo({
					url: '/pages/taskMg/arrangeTask/addArrageTask/listData/listData?type=' + v,
					events: {
						jieshouData: function(data) {
							console.log('jieshouData', data);
						},
						someEvent: function(data) {
							console.log('someEvent', data);
						},
					},
					success: function(res) {
						// 通过eventChannel向被打开的页面传送数据
						res.eventChannel.emit('jieshouData', {
							data: 'ddd'
						})
					}
				})
			},
			// 保存
			saveHandler() {
				let params = {
					id: this.id,
					taskConfigId: uni.getStorageSync('taskNameId'),
					taskStartTime: this.taskStartTime,
					taskEndTime: this.taskEndTime,
					responsibleUserId: uni.getStorageSync('leaderId'),
					carbonCopyUserIds: uni.getStorageSync('csManIds')
				}
				if (params.id == null) {
					// 新增
					this.addTaskInfoApi(params)
				} else {
					// 修改
					this.editTaskInfoApi(params)
				}
				// console.log('save params',params);

			},
			/**
			 * 接口
			 */
			getManOptionsApi() {
				uni.request({
					url: baseURL + '/api/sys_user/getEnableUser',
					method: "GET",
					header: {
						// "content-type": "appLication/x-www-form-urlencoded",
						'Authorization': uni.getStorageSync('token')
					},
					success: (res) => {
						if (res.data.code == 200) {
							this.manOptions = res.data.data
						}
					}
				})
			},
			// 新增
			addTaskInfoApi(params) {
				console.log(params);
				uni.request({
					url: baseURL + '/api/task/info/add',
					method: "POST",
					data: params,
					header: {
						// "content-type": "appLication/x-www-form-urlencoded",
						'Authorization': uni.getStorageSync('token')
					},
					success: (res) => {
						console.log('addTaskInfoApi', res);
						if (res.data.code == 200) {
							// this.manOptions= res.data.data
							uni.navigateTo({
								url: '/pages/taskMg/taskMg'
							})
							uni.showToast({
								title: '新增成功',
								icon: 'success', //如果要纯文本，不要icon，将值设为'none'
								duration: 1000 //持续时间为 2秒
							})
							uni.showToast({
								title: '修改成功',
								icon: 'success', //将值设置为 success 或者 ''
								duration: 1000 //持续时间为 2秒
							})
							uni.removeStorage({
								key: 'taskName',
								success: function() {
									console.log('移除成功');
								}
							})
							uni.removeStorage({
								key: 'taskNameId',
								success: function() {
									console.log('移除成功');
								}
							})
							uni.removeStorage({
								key: 'leader',
								success: function() {
									console.log('移除成功');
								}
							})
							uni.removeStorage({
								key: 'leaderId',
								success: function() {
									console.log('移除成功');
								}
							})
							uni.removeStorage({
								key: 'csMan',
								success: function() {
									console.log('移除成功');
								}
							})
							uni.removeStorage({
								key: 'csManIds',
								success: function() {
									console.log('移除成功');
								}
							})
						} else {
							uni.showToast({
								title: res.data.message,
								icon: '', //将值设置为 success 或者 ''
								duration: 1000 //持续时间为 2秒
							})
						}
					}
				})
			},
			// 修改
			editTaskInfoApi(params) {
				console.log(params);
				uni.request({
					url: baseURL + '/api/task/info/edit',
					method: "POST",
					data: params,
					header: {
						// "content-type": "appLication/x-www-form-urlencoded",
						'Authorization': uni.getStorageSync('token')
					},
					success: (res) => {
						console.log('editTaskInfoApi', res);
						if (res.data.code == 200) {
							// this.manOptions= res.data.data
							uni.navigateTo({
								url: '/pages/taskMg/taskMg'
							})
							uni.showToast({
								title: '修改成功',
								icon: 'success', //将值设置为 success 或者 ''
								duration: 1000 //持续时间为 2秒
							})
							uni.removeStorage({
								key: 'taskName',
								success: function() {
									console.log('移除成功');
								}
							})
							uni.removeStorage({
								key: 'taskNameId',
								success: function() {
									console.log('移除成功');
								}
							})
							uni.removeStorage({
								key: 'leader',
								success: function() {
									console.log('移除成功');
								}
							})
							uni.removeStorage({
								key: 'leaderId',
								success: function() {
									console.log('移除成功');
								}
							})
							uni.removeStorage({
								key: 'csMan',
								success: function() {
									console.log('移除成功');
								}
							})
							uni.removeStorage({
								key: 'csManIds',
								success: function() {
									console.log('移除成功');
								}
							})
						} else {
							uni.showToast({
								title: res.data.message,
								icon: '', //将值设置为 success 或者 ''
								duration: 1000 //持续时间为 2秒
							})
						}
					}
				})
			},
		}
	}
</script>
<style lang="scss">
	.box {
		padding: 40rpx;
		height: 95vh;
		// height: calc(100vh - 80rpx);
		font-size: 1rem;
		background-color: white;
		overflow-y: auto;
	}

	.row {
		width: calc(100vw - 80rpx);

		margin-bottom: 40rpx;
		height: 50rpx;
		display: flex;
		flex-direction: row;

		.row-left {
			width: 30%;
			height: 100%;
			;
		}

		.row-right {
			width: 70%;
			height: 100%;
			border-bottom: 1rpx solid #d6d6d6;
			display: flex;
			flex-direction: row;
			justify-content: space-between;

			.value {
				width: 70%;
				height: 100%;

				.uni-input {
					width: 100%;
					height: 60rpx;
				}
			}

			.tss {
				width: 30%;
				text-align: right;
				font-size: 0.8rem;
				color: #6c6c6c;

			}
		}
	}

	.btnBox {
		position: fixed;
		bottom: 3%;
		// z-index: 10;

		button {
			width: 90vw;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 40rpx;
			background-color: #409EFF;
			color: white;
			// margin-left: 5vw;
		}
	}
</style>